package ch.bfh.abcmmh.view.fullview.scopepanel;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import ch.bfh.abcmmh.view.GUIController;
import ch.bfh.abcmmh.view.fullview.scopepanel.arrow.AWTArrow;

public class ScopePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private Dimension dimension;
	private GUIController guiController;
	private float divisor;
	private int scopeWidth;
	private ScopeMouseDraggerListener draggerListener;
	private AWTArrow leftArrow, rightArrow;
	
	public ScopePanel(GUIController guiController) {
		this.guiController = guiController;
		this.scopeWidth = calculateScopeWidth();
		this.dimension = new Dimension(this.scopeWidth,
				guiController.getFullStreetPanelHeight());
		this.setPreferredSize(dimension);
		this.setSize(dimension);
		this.guiController = guiController;
		this.draggerListener = new ScopeMouseDraggerListener(this);
		this.addMouseListener(draggerListener);
		this.addMouseMotionListener(draggerListener);
		this.setLayout(new BorderLayout());

		
		this.setDoubleBuffered(true);
		
		this.setOpaque(false);
		this.setVisible(true);

		this.addArrows();
		
		this.updateScopePanel();
	}

	private void addArrows() {

		ImageIcon left = new ImageIcon(getClass().getResource("/arrowleft.jpg"));
		ImageIcon right = new ImageIcon(getClass().getResource(
				"/arrowright.jpg"));		
		this.leftArrow = new AWTArrow(guiController, left);
		this.rightArrow = new AWTArrow(guiController, right);
		this.add(leftArrow, BorderLayout.WEST);
		this.add(rightArrow, BorderLayout.EAST);
		
	}

	private int calculateScopeWidth() {
		this.divisor = (float) this.guiController.getLogicalStreetLenth()
				/ (float) this.guiController.getWidth();
		this.guiController.setDivisor(divisor);
		return (int) (this.guiController.getWidth() / this.divisor);
	}

	public AWTArrow getLeftArrow() {
		return leftArrow;
	}

	public AWTArrow getRightArrow() {
		return rightArrow;
	}

	public int calculateStreetArrayPositionofScope() {
		int arraypos = (int) (divisor * (float) getLocation().getX());
		guiController.setDetailPosition(arraypos);
		// for DEBUG
		return arraypos;
	}

	public GUIController getGuiController() {
		return guiController;
	}

	public int getScopeWidth() {
		return scopeWidth;
	}

	public int getScopeX() {
		return (int) this.getLocation().getX();
	}

	public void updateScopePanel() {

		this.repaint();
		this.leftArrow.paint(leftArrow.getGraphics());
		this.rightArrow.paint(rightArrow.getGraphics());

	}

	public ScopeMouseDraggerListener getDraggerListener() {
		return draggerListener;
	}

}
